package juc;

import java.util.concurrent.CountDownLatch;

/*
减少计数CountDownLatch演示
 */
public class CountDownLatchDemo {

//  场景:6个同学先走，班长再锁门，使用CountDownLatch，设置初始值为６，并调用自减方法，在初始值为０以前，调用await方法
  public static void main(String[] args) throws InterruptedException {

    CountDownLatch countDownLatch = new CountDownLatch(6);

    for (int i = 1; i <= 6 ; i++) {
      new Thread(()->{
        System.out.println(Thread.currentThread().getName() + "号同学先走了");
//        减一
        countDownLatch.countDown();
      },String.valueOf(i)).start();
    }

//    等待
    countDownLatch.await();
    System.out.println(Thread.currentThread().getName() + "　班长锁门了");
  }
}
